package com.sigmatrix.h5.mq;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.dao.DisDealerDao;
import com.sigmatrix.h5.dao.InStorageDao;
import com.sigmatrix.h5.dao.ProductDao;
import com.sigmatrix.h5.dao.WarehouseDao;
import com.sigmatrix.h5.dto.WarehouseData;
import com.sigmatrix.h5.entity.DisDealer;
import com.sigmatrix.h5.entity.InStorage;
import com.sigmatrix.h5.entity.Product;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.InStorageService;
import com.sigmatrix.h5.utils.JsonUtils;
import com.sigmatrix.h5.utils.Utils;
import com.sigmatrix.h5.web.vo.InStorageAddVo;
import com.sigmatrix.h5.web.vo.InStorageAutoAddVo;
import com.sigmatrix.h5.web.vo.InStorageDetailAddVo;
import com.sigmatrix.h5.web.vo.InStorageDetailAutoAddVo;
import com.sigmatrix.h5.web.vo.InStorageUpLoadAutoDetailVo;
import com.sigmatrix.h5.web.vo.InStorageUpLoadDetailVo;
import com.sigmatrix.h5.web.vo.InStorageUpLoadVo;
import com.sigmatrix.h5.web.vo.OperateCodeUpLoadVo;

/**
 * 
 *@ClassName: InStorageConsumer
 *@Description: 自动收货入库
 *@author gaomingkai
 *@date 2018年1月19日 下午2:08:40
 */
@Component
public class InStorageConsumer {

	private static final Logger LOGGER = LoggerFactory.getLogger(InStorageConsumer.class);
	@Autowired
	private transient InStorageService inStorageService;
	@Autowired
	private transient ProductDao productDao;
	@Autowired
	private transient InStorageDao inStorageDao;
	@Autowired
	private transient DisDealerDao disDealersDao; 
	@Autowired
	private transient WarehouseDao warehouseDao;
	/**
	 * 
	 *@Title: inStorageListener
	 *@Description:自动收货入库
	 *@return void 
	 *@author gaomingkai
	 *@date 2018年1月19日 下午2:08:58
	 *@param msg
	 */
	@RabbitListener(queues="${rabbit.bindings[3].queue}")
	public void inStorageListener(org.springframework.amqp.core.Message msg){
		
		try {
			String jsonString = new String(msg.getBody());
			LOGGER.info("收货入库获取消费信息...{}", jsonString);
			InStorageAutoAddVo inStorageAutoAddVo = JsonUtils.jsonToPojo(jsonString, InStorageAutoAddVo.class);
			//新增入库单
			//获取转换InStorageAddVo
			InStorageAddVo inStorageVo = this.inStorageAddSwitch(inStorageAutoAddVo);
			LOGGER.info("获取转化后的新增收获入库单InStorageAddVo信息...{}", JSONObject.toJSONString(inStorageVo));
			//新增入库单 (判断入库单是否存在)
			Response addResponse = inStorageService.saveAuto(inStorageVo);
			LOGGER.info("新增收货入库单返回信息...{}", JSONObject.toJSONString(addResponse));
			//获取转换inStorageAutoAddVo
			InStorageUpLoadVo inStorageUpLoadVo = this.inStorageUpLoadVoSwitch(inStorageAutoAddVo);
			LOGGER.info("获取转化后的收货入库单上传参数InStorageUpLoadVo信息...{}", JSONObject.toJSONString(inStorageUpLoadVo));
			//收获入库上传   OSI
			Response upLoadResponse = inStorageService.updateAuto(inStorageUpLoadVo);
			LOGGER.info("上传收货入库单返回信息...{}", JSONObject.toJSONString(upLoadResponse));
			
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	/**
	 * 
	 *@Title: InStorageUpLoadVoSwitch
	 *@Description: 获取转换inStorageAutoAddVo
	 *@return InStorageUpLoadVo
	 *@author gaomingkai
	 *@date 2018年1月19日 下午4:08:01
	 *@param inStorageAutoAddVo
	 *@return
	 *@throws DaoException
	 */
	private InStorageUpLoadVo inStorageUpLoadVoSwitch(InStorageAutoAddVo inStorageAutoAddVo) throws DaoException {
		InStorageUpLoadVo inStorageUpLoadVo = new InStorageUpLoadVo();
		InStorage inStorage = inStorageDao.findByOrderCode(inStorageAutoAddVo.getOrderCode(), inStorageAutoAddVo.getEnterpriseCode());
		inStorageUpLoadVo.setWarehouseId(inStorage.getWarehouseId());
		//inStorageUpLoadVo.setUserId(inStorage.getCreatorId());//自动入库没有上传用户id
		inStorageUpLoadVo.setDealerId(inStorage.getDealerId());
		inStorageUpLoadVo.setChannelType(inStorage.getChannelType());
		inStorageUpLoadVo.setInStorageId(inStorage.getId());
		inStorageUpLoadVo.setEnterpriseCode(inStorageAutoAddVo.getEnterpriseCode());
		inStorageUpLoadVo.setDealerId(inStorage.getDealerId());
		inStorageUpLoadVo.setUserDealerId(inStorage.getDealerId());
		inStorageUpLoadVo.setStatus(inStorageAutoAddVo.getStatus());
		
		List<InStorageUpLoadDetailVo> detailList = new ArrayList<>();
		
		List<InStorageUpLoadAutoDetailVo> upLoadDetailList = inStorageAutoAddVo.getUpLoadDetailList();
		
		for (InStorageUpLoadAutoDetailVo vo : upLoadDetailList) {
			InStorageUpLoadDetailVo inStorageUpLoadDetailVo = new InStorageUpLoadDetailVo();
			Product product = productDao.findByCode(vo.getProductCode(), inStorageAutoAddVo.getEnterpriseCode());
			inStorageUpLoadDetailVo.setProductId(product.getId());
			List<OperateCodeUpLoadVo> operateCodeList =  new ArrayList<>();
			
			List<OperateCodeUpLoadVo> operateCodeAutoList = vo.getOperateCodeList();
			for (OperateCodeUpLoadVo autoVo : operateCodeAutoList) {
				OperateCodeUpLoadVo operateCodeUpLoadVo = new OperateCodeUpLoadVo();
				BeanUtils.copyProperties(autoVo, operateCodeUpLoadVo);
				operateCodeList.add(operateCodeUpLoadVo);
			}
			
			inStorageUpLoadDetailVo.setOperateCodeList(operateCodeList);
			detailList.add(inStorageUpLoadDetailVo);
		}
		inStorageUpLoadVo.setDetailList(detailList);
		return inStorageUpLoadVo;
	}
	
	/**
	 * 
	 *@Title: InStorageAddSwitch
	 *@Description: 获取转换InStorageAddVo
	 *@return InStorageAddVo
	 *@author gaomingkai
	 *@date 2018年1月19日 下午2:47:24
	 *@param inStorageAutoAddVo
	 *@return
	 */
	private  InStorageAddVo inStorageAddSwitch(InStorageAutoAddVo inStorageAutoAddVo) {
		try {
			InStorageAddVo inStorageVo = new InStorageAddVo();
			inStorageVo.setEnterpriseCode(inStorageAutoAddVo.getEnterpriseCode());
			inStorageVo.setUserId(inStorageAutoAddVo.getUserId());
			if (Utils.isEmpty(inStorageAutoAddVo.getUserDealerCode())) {
				inStorageVo.setUserDealerId(0);
			}
			else {
				DisDealer dealers = disDealersDao.findByCode(inStorageAutoAddVo.getUserDealerCode(), inStorageAutoAddVo.getEnterpriseCode());
				if (Utils.isNotEmpty(dealers)) {
					inStorageVo.setUserDealerId(dealers.getId());
				}
				else {
					inStorageVo.setUserDealerId(0);
				}
			}
		
			inStorageVo.setChannelType(inStorageAutoAddVo.getChannelType());
			inStorageVo.setOrderCode(inStorageAutoAddVo.getOrderCode());
			
			//企业仓储手持销售出库不传递仓库字段，所以分销这边需要根据经销商code去找此经销商所绑定的默认仓库，如果默认仓库不存，暂时去找经销商所绑定的其它仓库(正常应该是平台创建经销商会绑定一个默认仓库)
			Integer warehouseId = inStorageAutoAddVo.getWarehouseId();
			if (Utils.isEmpty(warehouseId) || warehouseId == 0) {
				List<WarehouseData> warehouseDatas = warehouseDao.findPartByDealerIdDefault(inStorageAutoAddVo.getEnterpriseCode(), inStorageVo.getUserDealerId());
				if (Utils.isEmpty(warehouseDatas)) {
					List<WarehouseData> warehouseDatas2 = warehouseDao.findPartByDealerId(inStorageAutoAddVo.getEnterpriseCode(), inStorageVo.getUserDealerId());
					if (Utils.isNotEmpty(warehouseDatas2)) {
						WarehouseData warehouseData = warehouseDatas2.get(0);
						inStorageVo.setWarehouseId(warehouseData.getWarehouseId());
					}
				}
				else {
					WarehouseData warehouseData = warehouseDatas.get(0);
					inStorageVo.setWarehouseId(warehouseData.getWarehouseId());
				}
			}
			
			else {
				inStorageVo.setWarehouseId(inStorageAutoAddVo.getWarehouseId());
			}
			
			
			inStorageVo.setSource(inStorageAutoAddVo.getSource());
			inStorageVo.setRemarks(inStorageAutoAddVo.getRemarks());
			inStorageVo.setInStorageType(inStorageAutoAddVo.getInStorageType());
			inStorageVo.setStatus(inStorageAutoAddVo.getStatus());
			
			List<InStorageDetailAddVo> detailList = new ArrayList<>();
			
			List<InStorageDetailAutoAddVo> detailListAutos = inStorageAutoAddVo.getDetailList();
			for (InStorageDetailAutoAddVo detailAutoAddVo : detailListAutos) {
				InStorageDetailAddVo inStorageDetailAddVo = new InStorageDetailAddVo();
				Product product = null;
				try {
					product = productDao.findByCode(detailAutoAddVo.getProductCode(), inStorageAutoAddVo.getEnterpriseCode());
				} catch (DaoException e) {
				}
				inStorageDetailAddVo.setProductId(product.getId());
				inStorageDetailAddVo.setPlanNum(detailAutoAddVo.getPlanNum());
				inStorageDetailAddVo.setPlanSpNum(detailAutoAddVo.getPlanSpNum());
				detailList.add(inStorageDetailAddVo);
			}
			inStorageVo.setDetailList(detailList);
			return inStorageVo;
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return null;
		
	}
	
	
}
